﻿using System;
using System.ServiceModel.Syndication;
using System.Xml;

namespace MediaRss
{
	public class MediaRssItem : SyndicationItem
	{
		#region private fields

		#endregion

		#region constructor
		public MediaRssItem()
		{
			Init();
		}

		public MediaRssItem(string title, string content, Uri itemAlternateLink, string id, DateTimeOffset lastUpdatedTime) :
			base(title, content, itemAlternateLink, id, lastUpdatedTime)
		{
			Init();
		}

		private void Init()
		{
			ScenesNode = new Scenes();
			StatusNode = new Status();
			RestrictionNode = new Restriction();
			PriceNode = new Price();
			LicenseNode = new License();
			PeerLinkNode = new PeerLink();
			SubTitleNode = new SubTitle();
			EmbedNode = new Embed();
			BacklinksNode = new Backlinks();
			ResponsesNode = new Responses();
			CommentsNode = new Comments();
			CommunityNode = new Community();
			ContentNode = new Content();
		}

		#endregion

		#region public properties

		public Content ContentNode { get; set; }
		public Community CommunityNode { get; set; }
		public Comments CommentsNode { get; set; }
		public Responses ResponsesNode { get; set; }
		public Backlinks BacklinksNode { get; set; }
		public Embed EmbedNode { get; set; }
		public SubTitle SubTitleNode { get; set; }
		public PeerLink PeerLinkNode { get; set; }
		public License LicenseNode { get; set; }
		public Price PriceNode { get; set; }
		public Restriction RestrictionNode { get; set; }
		public Status StatusNode { get; set; }
		public Scenes ScenesNode { get; set; }

		#endregion

		protected override bool TryParseElement(XmlReader reader, string version)
		{
			Boolean parsed = false;
			if (version == SyndicationVersions.Rss20 && reader.NamespaceURI == MediaRssBase.NS_URI)
			{
				switch (reader.LocalName)
				{
					case MediaRss.Content.ELEMENT_NAME:
						ContentNode = new Content();
						ContentNode.ReadXml(reader);
						parsed = true;
						break;
					case Community.ELEMENT_NAME:
						CommunityNode = new Community();
						CommunityNode.ReadXml(reader);
						parsed = true;
						break;
					case Comments.ELEMENT_NAME:
						CommentsNode = new Comments();
						CommentsNode.ReadXml(reader);
						parsed = true;
						break;
					case Responses.ELEMENT_NAME:
						ResponsesNode = new Responses();
						ResponsesNode.ReadXml(reader);
						parsed = true;
						break;
					case Backlinks.ELEMENT_NAME:
						BacklinksNode = new Backlinks();
						BacklinksNode.ReadXml(reader);
						parsed = true;
						break;
					case Embed.ELEMENT_NAME:
						EmbedNode = new Embed();
						EmbedNode.ReadXml(reader);
						parsed = true;
						break;
					case Price.ELEMENT_NAME:
						PriceNode = new Price();
						PriceNode.ReadXml(reader);
						parsed = true;
						break;
					case License.ELEMENT_NAME:
						LicenseNode = new License();
						LicenseNode.ReadXml(reader);
						parsed = true;
						break;
					case PeerLink.ELEMENT_NAME:
						PeerLinkNode = new PeerLink();
						PeerLinkNode.ReadXml(reader);
						parsed = true;
						break;
					case SubTitle.ELEMENT_NAME:
						SubTitleNode = new SubTitle();
						SubTitleNode.ReadXml(reader);
						parsed = true;
						break;
					case Restriction.ELEMENT_NAME:
						RestrictionNode = new Restriction();
						RestrictionNode.ReadXml(reader);
						parsed = true;
						break;
					case Status.ELEMENT_NAME:
						StatusNode = new Status();
						StatusNode.ReadXml(reader);
						parsed = true;
						break;
					case Scenes.ELEMENT_NAME:
						ScenesNode = new Scenes();
						ScenesNode.ReadXml(reader);
						parsed = true;
						break;
					default:
						parsed = base.TryParseElement(reader, version);
						break;
				}
			}
			return parsed;
		}


		protected override void WriteElementExtensions(XmlWriter writer, string version)
		{
			if (version == SyndicationVersions.Rss20)
			{
				if (ContentNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, MediaRss.Content.ELEMENT_NAME, MediaRssBase.NS_URI);
					ContentNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (CommunityNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, Community.ELEMENT_NAME, MediaRssBase.NS_URI);
					CommunityNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (CommentsNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, Comments.ELEMENT_NAME, MediaRssBase.NS_URI);
					CommentsNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (ResponsesNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, Responses.ELEMENT_NAME, MediaRssBase.NS_URI);
					ResponsesNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (BacklinksNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX,Backlinks.ELEMENT_NAME, MediaRssBase.NS_URI);
					BacklinksNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (EmbedNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, Embed.ELEMENT_NAME, MediaRssBase.NS_URI);
					EmbedNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (SubTitleNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, SubTitle.ELEMENT_NAME, MediaRssBase.NS_URI);
					SubTitleNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (PeerLinkNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, PeerLink.ELEMENT_NAME, MediaRssBase.NS_URI);
					PeerLinkNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (LicenseNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, License.ELEMENT_NAME, MediaRssBase.NS_URI);
					LicenseNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (PriceNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, Price.ELEMENT_NAME, MediaRssBase.NS_URI);
					PriceNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (RestrictionNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, Restriction.ELEMENT_NAME, MediaRssBase.NS_URI);
					RestrictionNode.WriteXml(writer);
					writer.WriteEndElement();
				}
				if (StatusNode != null)
				{
					writer.WriteStartElement(MediaRssBase.PREFIX, Status.ELEMENT_NAME, MediaRssBase.NS_URI);
					StatusNode.WriteXml(writer);
					writer.WriteEndElement();
				}
			}
			base.WriteElementExtensions(writer, version);
		}
	}
}